package com.live.shop.service;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.live.shop.model.CommentModel;
import com.live.shop.model.UserModel;
import com.live.shop.sql.UserDatabase;

import java.util.ArrayList;
import java.util.List;

public class CommentService extends AccessibilityService {
    UserModel userModel;
    List<CommentModel> list;
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        UserDatabase userDb = new UserDatabase(getApplicationContext());
        userModel = userDb.select();
        list = new ArrayList<>();
        //当系统成功连接到该AccessibilityService时,将调用此方法。主要用与一次性配置或调整的代码。
    }
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        //当系统监测到相匹配的AccessibilityEvent事件时,将调用此方法,在整个Service的生命周期中,该方法将被多次调用
        int eventType = event.getEventType();
        if(eventType == userModel.getFunctionEventCode() && userModel.getFunctionPackage().equals(event.getPackageName())){
            AccessibilityNodeInfo windowNodeinfo = getRootInActiveWindow();
            List<AccessibilityNodeInfo> dynodes = windowNodeinfo.findAccessibilityNodeInfosByViewId(userModel.getFunctionResid());
            switch (userModel.getFunctionId()){
                case 1://抖音
                    if(dynodes.size() > 0){
                        sendDYMsg(dynodes);
                    }
                    break;
                case 2://小红书
                    sendXHSMsg(dynodes);
                    break;
                case 3://快手

                    break;
                case 4://视频号
                    if(dynodes.size() > 0){
                        sendDYMsg(dynodes);
                    }
                    break;
                case 5://拼多多
                    sendPDDMsg(dynodes);
                    break;
                case 6://淘宝

                    break;
                case 7://b站
                    if(dynodes.size() > 0){
                        sendBZMsg(dynodes);
                    }
                    break;
                case 8://京东
                    if(dynodes.size() > 0){
                        sendDYMsg(dynodes);
                    }
                    break;
            }
        }
    }
    //拼多多
    public void sendPDDMsg(List<AccessibilityNodeInfo> nodes){
        for (AccessibilityNodeInfo info:nodes) {
            if(info.getChildCount()  > 0){
                int commentViewPageIndex = -1;
                for(int index = 0 ;index <info.getChildCount(); index++){
                    //找出大于等于8个子节点的
                    if(info.getChild(index).getChildCount() >= 19 && info.getChild(index).getClassName().toString().equals("android.support.v4.view.ViewPager")){
                        commentViewPageIndex = index;
                        break;
                    }
                }
                if(commentViewPageIndex != -1){
                    AccessibilityNodeInfo node1 = info.getChild(commentViewPageIndex);
                    //找到android.widget.FrameLayout
                    int commentFrameLayoutIndex = -1;
                    for(int index = 0 ;index <node1.getChildCount(); index++){
                        //找出大于等于8个子节点的
                        if(node1.getChild(index).getChildCount() >= 3 &&node1.getChild(index).getClassName().toString().equals("android.widget.FrameLayout")){
                            commentFrameLayoutIndex = index;
                            break;
                        }
                    }
                    if(commentFrameLayoutIndex != -1){
                        //找到android.support.v7.widget.RecyclerView
                        AccessibilityNodeInfo node2 = node1.getChild(commentFrameLayoutIndex);
                        int commentRecyclerViewIndex = -1;
                        for (int index = 0;index < node2.getChildCount();index++) {
                            if(node2.getChild(index).getClassName().toString().equals("android.support.v7.widget.RecyclerView")){
                                commentRecyclerViewIndex = index;
                                break;
                            }
                        }
                        if(commentRecyclerViewIndex != -1){
                            AccessibilityNodeInfo node3 = node2.getChild(commentRecyclerViewIndex);
                            if(node3.getChildCount() > 0){
                                for(int a = 0 ; a < node3.getChildCount(); a++){
//                                    Log.d(GetUtil.TAG,"==========开始==========");
//                                    AccessibilityUtils.printNodeInfo(node3);
//                                    Log.d(GetUtil.TAG,"==========结束==========");
                                    if(node3.getChild(a) == null || node3.getChild(a).getChildCount() == 0) continue;
                                    AccessibilityNodeInfo node4 = node3.getChild(a).getChild(0);
                                    String comment = node4 != null ?node4.getText().toString():"";
//                                    Log.d(GetUtil.TAG,"【拼多多】"+comment);

                                    if(!comment.equals("")){
                                        String userName = "";
                                        String userCom = "";
                                        int type = 0;
                                        if(comment.contains(" 关注了主播")){
                                            type = 3;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                                            Log.d(GetUtil.TAG,"【关注】"+comment);
                                            userName = comment.replace(" 关注了主播","").trim();
                                            userCom = "关注了主播";
                                        }else if(comment.contains(" 送出")){
                                            type = 4;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                                            Log.d(GetUtil.TAG,"【礼物】"+comment);
                                            userName = comment.split(" 送出")[0];
                                            userCom = "送出礼物"+comment.split(" 送出")[1];
                                        }else if(comment.contains(" 来了")){
                                            type = 1;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                                            Log.d(GetUtil.TAG,"【来了】"+comment);
                                            userName = comment.replace("来了","").trim();
                                            userCom = "进入直播间";
                                        }else if(comment.split("：").length == 2){
                                            type = 2;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
                                            userName = comment.split("：")[0];
                                            userCom = comment.split("：")[1];
                                        }

                                        boolean isHas = false;
                                        if(list.size() < 14){
                                            for (int i = 0 ; i < list.size();i++) {
                                                if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                                                    isHas = true;
                                                    break;
                                                }
                                            }
                                        }else{
                                            for (int i = list.size()-15 ; i < list.size() && i > 0 ;i++) {
                                                if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                                                    isHas = true;
                                                    break;
                                                }
                                            }
                                        }

                                        if(!isHas && !userName.isEmpty() && !userCom.isEmpty()){
                                            CommentModel commentModel = new CommentModel();
                                            commentModel.setCommentsUser(userName);
                                            commentModel.setCommentsTxt(userCom);
                                            commentModel.setCommentsType(type);
                                            commentModel.setCommentsId(0);
                                            commentModel.setKeywordId(0);
                                            commentModel.setKeywordMate(0);
                                            commentModel.setKeywordReply(0);
                                            list.add(commentModel);
//                                            Log.d(GetUtil.TAG,"【发送】"+comment);
                                            Intent intent = new Intent("AccessibilityServiceComment");
                                            intent.putExtra("userCom", userCom);
                                            intent.putExtra("userName", userName);
                                            intent.putExtra("type", type);//类型 1用户进直播间 2评论
                                            intent.putExtra("function_id", userModel.getFunctionId());
                                            sendBroadcast(intent);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    //小红书
    public void sendXHSMsg(List<AccessibilityNodeInfo> nodes){
        for (AccessibilityNodeInfo info:nodes) {
            if(info.getChildCount()  > 0){
                AccessibilityNodeInfo node1 = info.getChild(0);
                if(node1.getChildCount()  > 0){
                    AccessibilityNodeInfo node2 = node1.getChild(0);
                    //找到androidx.recyclerview.widget.RecyclerView
                    int commentRecyclerViewIndex = -1;
                    for (int index = 0;index < node2.getChildCount();index++) {
                        if(node2.getChild(index).getClassName().toString().equals("androidx.recyclerview.widget.RecyclerView")){
                            commentRecyclerViewIndex = index;
                            break;
                        }
                    }
                    if(commentRecyclerViewIndex != -1){
                        AccessibilityNodeInfo node3 = node2.getChild(commentRecyclerViewIndex);
                        if(node3.getChildCount() > 0){
                            for(int a = 0 ; a < node3.getChildCount(); a++){
//                                Log.d(GetUtil.TAG,"==========开始==========");
//                                AccessibilityUtils.printNodeInfo(node3);
//                                Log.d(GetUtil.TAG,"==========结束==========");
                                String comment = node3.getChild(a).getText().toString();
//                                Log.d(GetUtil.TAG,"【小红书】"+comment);
                                if(!comment.equals("")){
                                    String userName = "";
                                    String userCom = "";
                                    int type = 0;
                                    if(comment.contains(" 关注了主播")){
                                        type = 3;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                                        Log.d(GetUtil.TAG,"【关注】"+comment);
                                        userName = comment.replace(" 关注了主播","").trim();
                                        userCom = "关注了主播";
                                    }else if(comment.contains(" 送出")){
                                        type = 4;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                                        Log.d(GetUtil.TAG,"【礼物】"+comment);
                                        userName = comment.split(" 送出")[0];
                                        userCom = "送出礼物"+comment.split(" 送出")[1];
                                    }else if(comment.contains(" 来了")){
                                        type = 1;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                                        Log.d(GetUtil.TAG,"【来了】"+comment);
                                        userName = comment.replace("来了","").trim();
                                        userCom = "进入直播间";
                                    }else if(comment.split("：").length == 2){
                                        type = 2;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
                                        userName = comment.split("：")[0];
                                        userCom = comment.split("：")[1];
                                    }
                                    boolean isHas = false;
                                    if(list.size() < 14){
                                        for (int i = 0 ; i < list.size();i++) {
                                            if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                                                isHas = true;
                                                break;
                                            }
                                        }
                                    }else{
                                        for (int i = list.size()-15 ; i < list.size() && i > 0 ;i++) {
                                            if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                                                isHas = true;
                                                break;
                                            }
                                        }
                                    }

                                    if(!isHas && !userName.isEmpty() && !userCom.isEmpty()){
                                        CommentModel commentModel = new CommentModel();
                                        commentModel.setCommentsUser(userName);
                                        commentModel.setCommentsTxt(userCom);
                                        commentModel.setCommentsType(type);
                                        commentModel.setCommentsId(0);
                                        commentModel.setKeywordId(0);
                                        commentModel.setKeywordMate(0);
                                        commentModel.setKeywordReply(0);
                                        list.add(commentModel);
//                                        Log.d(GetUtil.TAG,"【发送】"+comment);
                                        Intent intent = new Intent("AccessibilityServiceComment");
                                        intent.putExtra("userCom", userCom);
                                        intent.putExtra("userName", userName);
                                        intent.putExtra("type", type);//类型 1用户进直播间 2评论
                                        intent.putExtra("function_id", userModel.getFunctionId());
                                        sendBroadcast(intent);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    //B站
    public void sendBZMsg(List<AccessibilityNodeInfo> nodes){
        for (AccessibilityNodeInfo info:nodes) {
            String comment = info.getText().toString();
//            Log.d(GetUtil.TAG,"【B站】"+comment);
            String userName = "";
            String userCom = "";

            int type = 0;
            if(comment.contains(" 关注了主播")){
                type = 3;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                Log.d(GetUtil.TAG,"【B站关注】"+comment);
                userName = comment.replace(" 关注了主播","").trim();
                userCom = "关注了主播";
            }else if(comment.contains(" 送出")){
                type = 4;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                Log.d(GetUtil.TAG,"【B站礼物】"+comment);
                userName = comment.split(" 送出")[0];
                userCom = "送出礼物"+comment.split(" 送出")[1];
            }else if(comment.contains("来了")){
                type = 1;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                Log.d(GetUtil.TAG,"【B站来了】"+comment);
                userName = comment.replace("来了","").trim();
                userCom = "进入直播间";
            }else if(comment.split("：").length == 2){
                type = 2;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
                userName = comment.split("：")[0];
                userCom = comment.split("：")[1];
            }

            //判断list是否已存在该评论
            boolean isHas = false;
            if(list.size() < 14){
                for (int i = 0 ; i < list.size();i++) {
                    if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                        isHas = true;
                        break;
                    }
                }
            }else{
                for (int i = list.size()-15 ; i < list.size() && i > 0 ;i++) {
                    if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                        isHas = true;
                        break;
                    }
                }
            }

            if(!isHas && !userName.isEmpty() && !userCom.isEmpty()){
                CommentModel commentModel = new CommentModel();
                commentModel.setCommentsUser(userName);
                commentModel.setCommentsTxt(userCom);
                commentModel.setCommentsType(type);
                commentModel.setCommentsId(0);
                commentModel.setKeywordId(0);
                commentModel.setKeywordMate(0);
                commentModel.setKeywordReply(0);
                list.add(commentModel);
//                Log.d(GetUtil.TAG,"【发送】"+comment);

                Intent intent = new Intent("AccessibilityServiceComment");
                intent.putExtra("userCom", userCom);
                intent.putExtra("userName", userName);
                intent.putExtra("type", type);//类型 1用户进直播间 2评论
                intent.putExtra("function_id", userModel.getFunctionId());
                sendBroadcast(intent);
            }
        }
    }
    //抖音、视频号、京东
    public void sendDYMsg(List<AccessibilityNodeInfo> nodes){
        for (AccessibilityNodeInfo info:nodes) {
            String comment = info.getText().toString();

            String userName = "";
            String userCom = "";

            //【抖音】* Peaksemi 送出小心心*  x1
            //【抖音】* * 重 逢 关注了主播
            //【抖音】回归真心本性悟空 来了
            //【抖音】* 定风波：多少钱
            int type = 0;
            if(comment.contains(" 关注了主播")){
                type = 3;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                Log.d(GetUtil.TAG,"【关注】"+comment);
                userName = comment.replace(" 关注了主播","").trim();
                userCom = "关注了主播";
            }else if(comment.contains(" 送出")){
                type = 4;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                Log.d(GetUtil.TAG,"【礼物】"+comment);
                userName = comment.split(" 送出")[0];
                userCom = "送出礼物"+comment.split(" 送出")[1];
            }else if(comment.contains(" 来了")){
                type = 1;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
//                Log.d(GetUtil.TAG,"【来了】"+comment);
                userName = comment.replace("来了","").trim();
                userCom = "进入直播间";
            }else if(comment.split("：").length == 2){
                type = 2;//类型 1用户进直播间 2评论 3关注主播 4刷礼物
                userName = comment.split("：")[0];
                userCom = comment.split("：")[1];
            }
            //判断list是否已存在该评论
            boolean isHas = false;
            if(list.size() < 14){
                for (int i = 0 ; i < list.size();i++) {
                    if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                        isHas = true;
                        break;
                    }
                }
            }else{
                for (int i = list.size()-15 ; i < list.size() && i > 0 ;i++) {
                    if(list.get(i).getCommentsUser().equals(userName) && list.get(i).getCommentsTxt().equals(userCom)){
                        isHas = true;
                        break;
                    }
                }
            }

            if(!isHas && !userName.isEmpty() && !userCom.isEmpty()){
                CommentModel commentModel = new CommentModel();
                commentModel.setCommentsUser(userName);
                commentModel.setCommentsTxt(userCom);
                commentModel.setCommentsType(type);
                commentModel.setCommentsId(0);
                commentModel.setKeywordId(0);
                commentModel.setKeywordMate(0);
                commentModel.setKeywordReply(0);
                list.add(commentModel);
//                Log.d(GetUtil.TAG,"【发送】"+comment);

                Intent intent = new Intent("AccessibilityServiceComment");
                intent.putExtra("userCom", userCom);
                intent.putExtra("userName", userName);
                intent.putExtra("type", type);//类型 1用户进直播间 2评论 3关注主播 4刷礼物
                intent.putExtra("function_id", userModel.getFunctionId());
                sendBroadcast(intent);
            }
        }
    }
    @Override
    public void onInterrupt() {
        //系统需要中断AccessibilityService反馈时,将调用此方法。AccessibilityService反馈包括服务发起的震动、音频等行为。
    }
}